<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Wechat extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $wechat = new \wechat\WechatAccounts();

//        $wechat->checkSignature();
//        file_put_contents('test.txt',333);
//        $postArr = file_get_contents("php://input");
        // 处理消息类型，并设置回复类型和内容
//        $data = json_decode(json_encode(simplexml_load_string($postArr)),true);

        $data = $wechat->xmldecode(file_get_contents("php://input"));

        switch (strtolower($data['MsgType'])) {
            case 'event':
                $this->event($data);
                break;
            case 'text':
                $this->sendMsg('嗯嗯',$data['ToUserName'],$data['FromUserName']);
                break;
            default:
                ;
                break;
        }
    }

    private function event($data){

        switch (strtolower($data['Event'])){
            case 'subscribe':

                $this->sendMsg('欢迎关注',$data['ToUserName'],$data['FromUserName']);
                break;
            case 'scan':

                $this->sendMsg('欢迎再次光临~',$data['ToUserName'],$data['FromUserName']);
                break;
            case 'click':
                if($data['EventKey'] == 'one'){
                    $this->sendMsg('2',$data['ToUserName'],$data['FromUserName']);
                }else{
                    $this->sendMsg('I do ont know',$data['ToUserName'],$data['FromUserName']);

                }
                break;
            default:
                $this->sendMsg('欢迎回来',$data['ToUserName'],$data['FromUserName']);

                ;
                break;
        }

    }

    //发送消息
    public function sendMsg($msg,$toUser,$fromUser)
    {
        $time = time();
        $template = "<xml> 
        <ToUserName><![CDATA[%s]]></ToUserName> 
        <FromUserName><![CDATA[%s]]></FromUserName> 
        <CreateTime>%s</CreateTime> 
        <MsgType><![CDATA[%s]]></MsgType> 
        <Content><![CDATA[%s]]></Content> 
        </xml>";

        //发送消息角色互换
        $info = sprintf($template,$fromUser,$toUser, $time, 'text', $msg);

        echo $info;
        exit;
    }
    public function accounts(){
        $wchat = new \wechat\WechatAccounts();

        $data = '{
                 "button":[
                     {    
                       
                          "name":"我要提问",
                           "sub_button": [
                            {
                                "type": "click", 
                                "name": "1+1", 
                                "key": "one"
                                
                            }, 
                            {
                                "type": "click", 
                                "name": "2+2", 
                                "key": "two"
                                
                            }
                  
                        ]
                     },
                     {    
                          "type":"scancode_push",
                          "name":"扫一扫",
                          "key":"sao"
                     },
                     {    
                          "type":"view",
                          "name":"个人中心",
                          "url":"http://wx.5b245.com/user"
                     },
                  ]
            }';
        $r = $wchat->SetMenu($data);
        dump($r);

    }

    //用户授权
    public function user()
    {
//        exit(input('echostr'));
        $wchat = new \wechat\WechatOauth();

        $code = input('code');
        $user = $wchat->getUserAccessUserInfo($code);

        dump($user);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
